package com.repast.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.repast.common.Const;
import com.repast.common.ServerResponse;
import com.repast.dao.DeskTimeMapper;
import com.repast.pojo.DeskTime;
import com.repast.service.IDeskService;
import com.repast.service.IDeskTimeService;
import com.repast.utils.CalculateDateUtils;

@Service
public class DeskTimeServiceImpl implements IDeskTimeService {

	@Autowired
	private DeskTimeMapper deskTimeMapper;

	public ServerResponse selectDeskTimeById(Integer deskId, Date date) {
		List<DeskTime> list = deskTimeMapper.selectDeskTimeById(deskId);
		if (list.size() == 0) {
			return ServerResponse.createBySuccess();
		}
		for (DeskTime deskTime : list) {
			String start = deskTime.getDeskTimeStartTime();
			//桌子已经被预约的前30分钟堂食都不能预订
			String startSubtract = CalculateDateUtils.subtractMinutes(30);
			String end = deskTime.getDeskTimeEndTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			try {
				if(start!=null&&end!=null){
					Date startSubtractTime = sdf.parse(startSubtract);
					Date endTime = sdf.parse(end);
					if(date.after(startSubtractTime) && date.before(endTime)){
						String data = new StringBuffer(start.substring(10)).append("~").append(end.substring(10)).toString();
						return ServerResponse.createByErrorCodeMessage(Const.ALREADY_APPOINTMENT, "桌子无法预约",data);
					}
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return ServerResponse.createBySuccessMessage("预约成功");
	}

}
